package com.mef.smsplat.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import com.mef.smsplat.dao.AirportInfoDao;
import com.mef.smsplat.dao.EtermConfigInfoDao;
import com.mef.smsplat.dao.TempVarInfoDao;
import com.mef.smsplat.dao.TemplateInfoDao;
import com.mef.smsplat.domain.AirportInfoDomain;
import com.mef.smsplat.domain.EtermConfigInfoDomain;
import com.mef.smsplat.domain.TempVarInfoDomain;
import com.mef.smsplat.domain.TemplateInfoDomain;
import com.mef.smsplat.eterm.Eterm;
import com.mef.smsplat.util.FreeMarkerUtil;
import com.sun.xml.internal.bind.v2.runtime.reflect.ListTransducedAccessorImpl;

@Service
public class CacheService {
	private static final Log logger=LogFactory.getLog(CacheService.class);
	
	private TempVarInfoDao tempVarInfoDao;
	private AirportInfoDao airportInfoDao;
	private EtermConfigInfoDao etermConfigInfoDao;
	private TemplateInfoDao templateInfoDao;
	private CacheManager cacheManager;
	

	public void setTempVarInfoDao(TempVarInfoDao tempVarInfoDao) {
		this.tempVarInfoDao = tempVarInfoDao;
	}
	
	public void setAirportInfoDao(AirportInfoDao airportInfoDao) {
		this.airportInfoDao = airportInfoDao;
	}
	

	public void setEtermConfigInfoDao(EtermConfigInfoDao etermConfigInfoDao) {
		this.etermConfigInfoDao = etermConfigInfoDao;
	}

	public void setTemplateInfoDao(TemplateInfoDao templateInfoDao) {
		this.templateInfoDao = templateInfoDao;
	}

	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}
	
	@Transactional(readOnly=true)
	public void initCache(){
		Cache cache=cacheManager.getCache("staticCache");//永不销毁的Cache
		cache.removeAll();//清除所有
		
		logger.info("===============加载机场信息到缓存=================");
		List<AirportInfoDomain> airportList = airportInfoDao.findAll();		
		for(int i=0;i<airportList.size();i++){
			AirportInfoDomain domain=airportList.get(i);
			Element e=new Element(domain.getCode(), domain);//key-val==code-domain
			cache.put(e);//放入缓存
		}
		
		logger.info("===============加载模版变量信息到缓存=================");
		List<TempVarInfoDomain> Varlist=tempVarInfoDao.findAll();	
		Element e=new Element("varList", Varlist);//把模版变量列表放入缓存
		cache.put(e);
		
		logger.info("===============加载模版信息=================");
		List<TemplateInfoDomain> tempList= templateInfoDao.findAll();
		FreeMarkerUtil.refreshTemplate(tempList);
		
		logger.info("===============刷新Eterm配置=================");
		Eterm.refreshConfig();
	}
	
	public List<TempVarInfoDomain> getTempVar(){
		Cache cache=cacheManager.getCache("staticCache");
		Element e= cache.get("varList");
		if(e!=null)
			return (List<TempVarInfoDomain>) e.getObjectValue();
		return null;
	}
	
	/**
	 * 缓存统计
	 * @return
	 */
	public List<Map<String,Object>>  statCache(){
		Cache passCache = cacheManager.getCache("passCache");
		Cache staticCache = cacheManager.getCache("staticCache");
		
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		Map<String, Object> passMap=new HashMap<String, Object>();
		Map<String, Object> staticMap=new HashMap<String, Object>();
		
		passMap.put("cacheName", "passCache");
		passMap.put("cacheDesc", "旅客缓存");
		passMap.put("cacheSize", passCache.getSize());//缓存对象数
		passMap.put("memoryStoreSize", passCache.getMemoryStoreSize());//缓存占用内存大小
		passMap.put("cacheHits", passCache.getStatistics().getCacheHits());//命中次数
		passMap.put("chacheMisses", passCache.getStatistics().getCacheMisses());//错失次数
		
		staticMap.put("cacheName", "staticCache");
		staticMap.put("cacheDesc", "机场+模版变量缓存");
		staticMap.put("cacheSize", staticCache.getSize());//缓存对象数
		staticMap.put("memoryStoreSize", staticCache.getMemoryStoreSize());//缓存占用内存大小
		staticMap.put("cacheHits", staticCache.getStatistics().getCacheHits());//命中次数
		staticMap.put("chacheMisses", staticCache.getStatistics().getCacheMisses());//错失次数
		
		list.add(passMap);
		list.add(staticMap);
		
		return list;
	}
	
	/**
	 * Eterm发送指令数统计
	 * @return
	 */
	public List<EtermConfigInfoDomain> statEterm(){
		return etermConfigInfoDao.findAll();
	}
}
